<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
    <style>
        *{
            margin: 0px;
            padding: 0px;
        }
        .code,button{
            width:200px;
            height: 46px;
            line-height: 46px;
            text-align: center;
            border: 1px solid #333;
            margin-bottom: 30px;
        }
    </style>
</head>
<body>
    <button id="btn">获取验证码</button>
    <div class="code" id="code"></div>
</body>
</html>
<script>
    let btn=document.getElementById("btn");
    let code=document.getElementById("code");
    let codeText="1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM";
    let len=5;
    let codeText2="1234567890";
    function getCode(codeText,len) {
        // 验证码的结果
        let res = "";
        while(res.length<4){
            let index = Math.round(Math.random() * (codeText.length-1));
            let item=codeText[index];
            if(res.indexOf(item)==-1){
                res+=item;
            }
           
        }
        return res;
    }
    code.innerText=getCode(codeText2,len);
    btn.onclick=function(){
        code.innerText=getCode(codeText2,len);
    }
    
</script>